﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Workflow.Runtime;
using System.Collections.ObjectModel;
using ChinaCustoms.Qingdao.Workflow.Supports;
using System.Web.Util;
using ChinaCustoms.Qingdao.Workflow.Controls;
using ChinaCustoms.Qingdao.Workflow.Permissions;

public partial class FunctionPage : System.Web.UI.Page
{
    string userName;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.User.Identity.IsAuthenticated)
            Response.Redirect("Default.aspx", true);
        else
            userName = PermissionUtils.StripIrrespectiveCharsForLogOnName(this.User.Identity.Name);

        MyExecutingWorkItemsRepeater.ItemDataBound += new RepeaterItemEventHandler(MyExecutingWorkItemsRepeater_ItemDataBound);
        UserParticipantTasksRepeater.ItemDataBound += new RepeaterItemEventHandler(UserParticipantTasksRepeater_ItemDataBound);
        if (!IsPostBack)
        {
            string applicationCodeName = ConfigurationManager.AppSettings["ApplicationCodeName"];

            //1.使用新的灵活的方法,按照开发者的需求,提取符合任意条件的 待办事项
            //灵活性的实现,是可以让开发人员给 workItemQuery的各个属性赋值来实现的
            WorkItemQuery workItemQuery = new WorkItemQuery();
            workItemQuery.ApplicationCodeName = applicationCodeName;
            workItemQuery.ExecutorUserName = userName;
            workItemQuery.WorkItemStatus = WorkItemStatus.Executing;
            workItemQuery.TaskStatus = TaskStatus.Executing;
            MyExecutingWorkItemsRepeater.DataSource = WorkItems.GetWorkItems(workItemQuery).Records;
            MyExecutingWorkItemsRepeater.DataBind();

            //MyExecutingWorkItemsRepeater.DataSource = WorkItems.GetUserExecutingWorkItems(applicationCodeName, userName);
            //MyExecutingWorkItemsRepeater.DataBind();

            //2.使用新的灵活的方法,按照开发者的需求,提取符合任意条件的 已办事项
            //灵活性的实现,是可以让开发人员给 taskQuery的各个属性赋值来实现的
            TaskQuery taskQuery = new TaskQuery();
            taskQuery.WorkItemStatuses = new WorkItemStatus[] { WorkItemStatus.Cancelled, WorkItemStatus.Completed };
            taskQuery.WorkItemExecutorUserName = userName;
            taskQuery.ApplicationCodeNames = new string[] { applicationCodeName };
            taskQuery.SortBy = SortTaskBy.CreateDate;
            UserParticipantTasksRepeater.DataSource = Tasks.GetTasks(taskQuery).Records;
            UserParticipantTasksRepeater.DataBind();

            //UserParticipantTasksRepeater.DataSource = Tasks.GetUserParticipantTasks(userName, applicationCodeName);
            //UserParticipantTasksRepeater.DataBind();
        }
    }


    void MyExecutingWorkItemsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.AlternatingItem:
            case ListItemType.Item:
                WorkItemInfo item = e.Item.DataItem as WorkItemInfo;
                if (item != null)
                {
                    Literal applicationName = e.Item.FindControl("ApplicationName") as Literal;
                    if (applicationName!=null)
                        applicationName.Text = item.Task.ApplicationName;

                    HyperLink taskName = e.Item.FindControl("TaskName") as HyperLink;
                    if (taskName != null)
                    {
                        taskName.Text = item.Task.TaskName;
                        taskName.NavigateUrl = String.Format(item.Activity.UrlOfWorkItem + "?ActivityCodeName={0}&WorkItemGuid={1}&TaskGuid={2}", item.Activity.CodeName, item.WorkItemGuid, item.TaskGuid);
                    }

                    Literal executingActivityName = e.Item.FindControl("ExecutingActivityName") as Literal;
                    if (executingActivityName != null)
                    {
                        executingActivityName.Text = item.ActivityName;
                    }

                    Literal creatorDisplayName = e.Item.FindControl("CreatorDisplayName") as Literal;
                    if (creatorDisplayName != null)
                    {
                        creatorDisplayName.Text = item.CreatorDisplayName;
                    }

                    Literal creatorOrganization = e.Item.FindControl("CreatorOrganization") as Literal;
                    if (creatorOrganization != null)
                    {
                        creatorOrganization.Text = PermissionUtils.StripDisplayNameFromUserFullPath(item.CreatorFullPath);
                    }

                    Literal createDate = e.Item.FindControl("CreateDate") as Literal;
                    if (createDate != null)
                    {
                        createDate.Text = item.CreateDate.ToString();
                    }
                }
                break;
        }
    }


    void UserParticipantTasksRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.AlternatingItem:
            case ListItemType.Item:
                TaskInfo task = e.Item.DataItem as TaskInfo;
                if (task != null)
                {
                    Literal applicationName = e.Item.FindControl("ApplicationName") as Literal;
                    if (applicationName != null)
                        applicationName.Text = task.ApplicationName;

                    HyperLink taskName = e.Item.FindControl("TaskName") as HyperLink;
                    if (taskName != null)
                    {
                        taskName.Text = task.TaskName;
                        ActivityPrototypeInfo firstActivityPrototype = ActivityPrototypes.GetFirstActivityPrototypeOfMainFlow(task.WorkflowGuid);

                        taskName.NavigateUrl = String.Format(firstActivityPrototype.UrlOfWorkItem + "?TaskGuid={0}", task.TaskGuid);
                    }

                    Literal executingActivityName = e.Item.FindControl("ExecutingActivityName") as Literal;
                    if (executingActivityName != null)
                    {
                        executingActivityName.Text = task.ExecutingStepName;
                    }

                    Literal creatorDisplayName = e.Item.FindControl("CreatorDisplayName") as Literal;
                    if (creatorDisplayName != null)
                    {
                        creatorDisplayName.Text = task.SponsorDisplayName;
                    }

                    Literal creatorOrganization = e.Item.FindControl("CreatorOrganization") as Literal;
                    if (creatorOrganization != null)
                    {
                        creatorOrganization.Text = PermissionUtils.StripDisplayNameFromUserFullPath(task.SponsorFullPath);
                    }

                    Literal createDate = e.Item.FindControl("CreateDate") as Literal;
                    if (createDate != null)
                    {
                        createDate.Text = task.CreateDate.ToString();
                    }

                    Literal status = e.Item.FindControl("Status") as Literal;
                    if (status != null)
                        status.Text = task.StatusText;
                }
                break;
        }
    }


    protected void CreateApprovalTask_Click(object sender, EventArgs e)
    {
        Response.Redirect("WorkItem.aspx?StartWorkflow=true", true);
    }

}
